package main

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"hash/fnv"
	"time"
)

const (
	mysql_user   = "root"
	mysql_passwd = "tjbkxl"
	mysql_ip     = "my2"
	mysql_port   = "3306"
	mysql_qyDB   = "dt_log"
)

func main() {
	start := time.Now()
	funReadSql()
	elapsed := time.Since(start)
	fmt.Println("App elapsed: ", elapsed)
}

func testHash(val string, partitionNum int32) int32 {
	hasher := fnv.New32a()
	hasher.Write([]byte(val))
	hash := hasher.Sum32()
	p := int32(hash)
	if p < 0 {
		p = -p
	}
	i := p % partitionNum
	return i
}

func funReadSql() () {
	db, err := sql.Open("mysql", mysql_user+":"+mysql_passwd+"@tcp("+mysql_ip+":"+mysql_port+")/"+mysql_qyDB+"?charset=utf8")
	if err != nil {
		fmt.Println("funReadSql Open is error")
	}
	rows, err := db.Query("SELECT ExchangeId FROM dt_file_log")
	if err != nil {
		fmt.Println("funReadSql SELECT t_knowledge_tree is error", err)
	}
	for rows.Next() {
		var ExchangeId string
		err := rows.Scan(&ExchangeId)
		if err != nil {
			fmt.Printf(err.Error())
			continue
		}
		hashVal := testHash(ExchangeId, 3);
		if hashVal != 2 {
			fmt.Println(ExchangeId)
		}
	}
	//关闭数据库
	db.Close()
}
